package menu;
import iterator.Iterator;

import java.util.Scanner;

import doublelinkedlist.DoubleLinkedList;

import visitor.PrintingVisitor;

import enumeration.Enumeration;


public class DoubleLinkedListMenu implements Menu {
	private DoubleLinkedList doublelinkedlist;

	public void show(DoubleLinkedList doublelinkedlist) {
		this.doublelinkedlist = doublelinkedlist;
		try {
			while (executeFunction(callMenu()))
				;
		} catch (NoSuchOptionException e) {
			e.printStackTrace();
		}
	}

	public Integer callMenu() {
		System.out.println("\n*********************************************\n");
		System.out.println("Selecione uma opção");
		System.out.println("1: Append");
		System.out.println("2: Prepend");
		System.out.println("3: Extract");
		System.out.println("4: Get First");
		System.out.println("5: Get Last");
		System.out.println("6: Is Empty?");
		System.out.println("7: Purge");
		System.out.println("8: Print DoubleLinkedList (Visitor)");
		System.out.println("9: Print DoubleLinkedList (Iterator)");
		System.out.println("10: Print DoubleLinkedList (Enumeration)");
		System.out.println("0: Exit");
		Scanner entrada = new Scanner(System.in);
		String option = entrada.nextLine();
		return new Integer(option);
	}

	public boolean executeFunction(Integer option) throws NoSuchOptionException {
		Scanner entrada = new Scanner(System.in);
		switch (option) {
		case 0:
			return false;
		case 1:
			System.out.println("Digite o valor a ser inserido no final da lista:");
			String valor = entrada.next();
			doublelinkedlist.append(valor);
			break;
		case 2:
			System.out.println("Digite o valor a ser inserido no início da lista:");
			String valor2 = entrada.next();
			doublelinkedlist.prepend(valor2);
			break;
		case 3:
			System.out.println("Digite o valor a ser inserido no início da lista:");
			String removedValue = entrada.next();
			doublelinkedlist.extract(removedValue);
			System.out.println("Elemento " + removedValue
					+ " removido da pilha.");
			break;
		case 4:
			System.out.println("First: " + doublelinkedlist.getFirst() + ".");
			break;	
		case 5:
			System.out.println("Last: " + doublelinkedlist.getLast() + ".");
			break;	
		
		case 6:
			if (doublelinkedlist.isEmpty())
				System.out.println("A pilha está vazia.");
			else
				System.out.println("A pilha não está vazia.");
			break;
		case 7:
			System.out
					.println("A pilha foi reiniciada e todos seus elementos removidos.");
			doublelinkedlist.purge();
			break;
		case 8:
			System.out.println("Elementos da pilha: ");
			doublelinkedlist.accept(new PrintingVisitor());
			break;
		case 9:
			Iterator iterator = doublelinkedlist.getIterator();
			for (iterator.init(); iterator.test(); iterator.inc())
				System.out.println(iterator.current());
			break;
		case 10:
			Enumeration enumeration = doublelinkedlist.getEnumeration();
			while (enumeration.hasMoreElements()) {
				System.out.println(enumeration.nextElement());
			}
			break;
		default:
			throw new NoSuchOptionException("Opção inválida");
		}
		return true;
	}
}
